package config.java;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ConfigAdmin extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ConfigAdmin() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	private String showConfig(){
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		ConfigValue value = ConfigValue.getInstance();
		Hashtable<String,ConfigBin> conf = value.getConf();
		StringBuffer sb = new StringBuffer();
		sb.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		sb.append("<HTML>");
		sb.append(" <HEAD><TITLE>Config Admin</TITLE>");
		sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></HEAD>");
		sb.append("  <BODY>");
		sb.append("<center><p><h3>配置文件列表</h3>");
		sb.append("<table width=100% cellpadding=5 cellspacing=0 border=1>");
		sb.append("<tr><td align=center>别名</td><td align=center>文件名</td><td align=center>上次载入时间</td><td align=center>上次载入状态</td>");
		sb.append("<td align=center>失败原因</td></tr>");
		for(Iterator itr = conf.keySet().iterator(); itr.hasNext();){ 
			String key = (String) itr.next(); 
			ConfigBin b = conf.get(key);
			sb.append("<tr><td align=center>");
			sb.append(b.getAlias());
			sb.append("</td><td align=center>");
			sb.append(b.getFile());
			sb.append("</td><td align=center>");
			sb.append(fmt.format(new Date(b.getLoadTime())));
			sb.append("</td><td align=center>");
			sb.append(b.getStatus() == 1 ? "成功" : "失败");
			sb.append("</td><td align=center>");
			sb.append(b.getStatus() == -1 ? b.getError() :"&nbsp;");
			sb.append("</td></tr>");
		}
		sb.append("</table>");
		sb.append("</center>");
		sb.append("  </BODY>");
		sb.append("</HTML>");
		return sb.toString();
	}
	
	
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.print(showConfig());
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
